package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.flag.GameDefine;
	import engine.flag.ObjType;
	import engine.flag.ResourcesPath;
	import engine.logic.LogSendLogic;
	import engine.logic.ShowDamageNum;
	import engine.logic.tips.TipsManage;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.RoleModel;
	import engine.tip.TipsMainLogic;
	import engine.ui.CDClearTypeUI;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.utils.FilterUtil;
	
	/**
	 * 世界Boss 
	 * @author gf
	 * 
	 */	
	
	public class WorldBossBloodUI extends BaseUI
	{
		private static var self			:WorldBossBloodUI;
		
		/**boss血条界面*/		
		public var RoleMc					:MovieClip;
		public var funcBtn1				:MovieClip;
		public var funcBtn2				:MovieClip;
		public var funcBtn3				:MovieClip;
		public var funcBtn4				:MovieClip;
		
		private var model					:ModelLocator;
		private var bind					:BindManage;
		public var npcModel				:BasePlayerModel;
		
		private var hpIndex				:int		=	-1;
		private var vipIndex				:int		=	-1;
		private var exciteTimesIndex		:int		=	-1;
		
		private var hpWidth				:Number		=	0;
		
		private var faceClip				:NxClip;
		
		/**是否自动战斗*/
		public var isAutoAttack			:Boolean	=	false;
		/**自动战斗操作间隔时间*/
		private var autoTime				:int		=	2000;
		public var autoIndex				:int		=	-1;
		
		public var isDead					:Boolean	=	false;
		
		public var isHideBuffs				:Boolean	=	false;
		
		public var damageEffect			:MovieClip;
		
		private var bossHP					:int		=	-1;
		
		private var isFirst				:Boolean	=	true;
		
		private var lastHP					:int;
		
		/**间隔时间*/		
		private var damageTime				:int		=	1000;
		
		private var lastTime				:int;
		
		public function WorldBossBloodUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():WorldBossBloodUI
		{
			if(self == null)
			{
				self 		= 	new WorldBossBloodUI();
			}
			return self;
		}
		
		
		override public function Init():void
		{
			model			=	ModelLocator.getInstance();
			bind			=	BindManage.getInstance();
			hpWidth			=	RoleMc["hpLine"].width;
			initBtns();
			damageEffect	=	new MovieClip();
			addChild(damageEffect);
		}
		
		override protected function BeforeShow():void
		{
			addEvents();
			if(npcModel	==	null)
			{
				RoleMc["lvText"].htmlText 		= 	"?";
				RoleMc["nameText"].htmlText 	= 	"???";
				
				faceClip						=	new NxClip();
				faceClip.isCache				=	true;
				RoleMc["face"].addChild(faceClip);
				faceClip.source = ResourcesPath.getHeadPic(49330100);
				
				RoleMc["hpText"].htmlText		=	"???/???" ;
				RoleMc["hpLine"].width			=	0;
				RoleMc["check"].visible		=	false;
				var state:int					=	isAutoAttack==true ? 2 : 1;
				RoleMc["check"].gotoAndStop(state);
			}
			vipIndex	=	bind.BindProp(model.scene.Role,"VIPLevel",this,"vipStatus");
			isAutoAttack	=	false;
			RoleMc["check"].gotoAndStop(1);
			exciteTimesIndex	=	bind.BindProp(model.scene.Role,"WorldBossExciteTimes",this,"onInspire");
		}
		
		override protected function AfterHide():void
		{
			removeEvents();
			if(hpIndex != -1)
			{
				bind.UnbindProp(npcModel.playerObj,"HP",hpIndex);
			}
			if(vipIndex != -1)
			{
				bind.UnbindProp(model.scene.Role,"VIPLevel",vipIndex);
			}
			if(exciteTimesIndex != -1)
			{
				bind.UnbindProp(model.scene.Role,"WorldBossExciteTimes",exciteTimesIndex);
			}
			npcModel	=	null;
			
			reset();
			isFirst		=	true;
			TipsMainLogic.instance.hide();
		}
		
		public function set onInspire(val:int):void
		{
			if(val == 5)
			{
				funcBtn1.buttonMode	=	funcBtn3.buttonMode	=	false;
				funcBtn1.filters	=	funcBtn3.filters	=	[FilterUtil.GrayColorFilter];
				funcBtn1.removeEventListener(MouseEvent.CLICK,onBtnClick);
				funcBtn3.removeEventListener(MouseEvent.CLICK,onBtnClick);
			}
			else
			{
				funcBtn1.buttonMode	=	funcBtn3.buttonMode	=	true;
				funcBtn1.filters	=	funcBtn3.filters	=	null;
				funcBtn1.addEventListener(MouseEvent.CLICK,onBtnClick);
				funcBtn3.addEventListener(MouseEvent.CLICK,onBtnClick);
			}
		}
		
		private function reset():void
		{
			while (damageEffect.numChildren > 0)
			{
				damageEffect.removeChildAt(0);
			}
			while(RoleMc["face"].numChildren >0)
			{
				RoleMc["face"].removeChildAt(0);
			}
		}
		
		private function initBtns():void
		{
			for(var i:int=1;i<=4;i++)
			{
				this["funcBtn"+i].name		=	"funcBtn"+i;
				this["funcBtn"+i].stop();
				this["funcBtn"+i].buttonMode	=	true;
				this["funcBtn"+i].visible	=	false;
			}
		}
		
		private function addEvents():void
		{
			for(var i:int=1;i<=4;i++)
			{
				addListeners(this["funcBtn"+i]);
			}
			RoleMc["check"].addEventListener(MouseEvent.CLICK,onAutoAttack);
		}
		
		private function removeEvents():void
		{
			for(var i:int=1;i<=4;i++)
			{
				removeListeners(this["funcBtn"+i]);
			}
			RoleMc["check"].removeEventListener(MouseEvent.CLICK,onAutoAttack);
		}
		
		private function onAutoAttack(e:MouseEvent):void
		{
			isAutoAttack		=	!isAutoAttack;
			if(isAutoAttack)
			{
				RoleMc["check"].gotoAndStop(2);
				autoIndex		=	setInterval(AutoAttack,autoTime);
			}
			else
			{
				RoleMc["check"].gotoAndStop(1);
				clearInterval(autoIndex);
			}
			LogSendLogic.getInstance().btnClickNumSend(22829);
		}
		
		public function autoAtack(boolean:Boolean):void
		{
			if(!boolean)
			{
				if(autoIndex != -1) clearInterval(autoIndex);
			}
			else
			{
				if(isAutoAttack)
				{
					if(autoIndex != -1) clearInterval(autoIndex);
					autoIndex		=	setInterval(AutoAttack,autoTime);
				}
			}
		}
		
		/**
		 * 自动复活攻击
		 */		
		private function AutoAttack():void
		{
			if(GameData.SceneType != GameDefine.SceneType_WorldBossScene)
			{
				autoAtack(false);
				return;
			}
			
			if( !isAutoAttack || ( model.scene.Role.GetProperties("WorldPlayerDead")==1 ))
			{
				GameData.Role.killMove();
				return;
			}
			var playerArr:Array	=	SceneManager.getInstance().norSceneView.scenePlayerLogic.playerDepts;
			for each( var npc : BasePlayerModel in playerArr )
			{
				if( npc is RoleModel )
				{
					continue;
				}
				if( npc.visible && npc.playerObj.Type == ObjType.NPC)
				{	
					npc.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
					break;
				}
			}
		}
		
		/**
		 * @param val 控制显示隐藏神符
		 */		
		public function hideBuffers(val:Boolean):void
		{
			isHideBuffs	=	val;
			var playerArr:Array	=	SceneManager.getInstance().norSceneView.scenePlayerLogic.playerDepts;
			for each( var npc : BasePlayerModel in playerArr )
			{
				if( npc is RoleModel )
				{
					continue;
				}
				if( npc.playerObj.Type == ObjType.HierogramNPC)
				{	
					if(val)
					{
						npc.mouseChildren = npc.mouseEnabled =  npc.visible = false;
					}
					else
					{
						npc.mouseChildren = npc.mouseEnabled =  npc.visible = npc.playerObj.GetPropertiesEx( "CantVisible" ) != 1;
					}
				}
			}
		}
		
		private function addListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(MouseEvent.CLICK,onBtnClick);
			dispatcher.addEventListener(MouseEvent.MOUSE_OVER,onBtnOver);
			dispatcher.addEventListener(MouseEvent.MOUSE_OUT,onBtnOut);
		}
		
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(MouseEvent.CLICK,onBtnClick);
			dispatcher.removeEventListener(MouseEvent.MOUSE_OVER,onBtnOver);
			dispatcher.removeEventListener(MouseEvent.MOUSE_OUT,onBtnOut);
		}
		
		private function onBtnClick(e:MouseEvent):void
		{
			switch(e.currentTarget.name)
			{
				case "funcBtn1"://阅历
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,12,1);
					LogSendLogic.getInstance().btnClickNumSend(22827)
					break;
				case "funcBtn2":
					if(model.scene.Role.GetProperties("WorldPlayerDead")==1)
					{
						if(CDClearTypeUI.getInstance()._isopen == 1)
						{
							SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD_CONSUME,11);//请求清除CD
						}
						else
						{
							SystemSender.getInstance().CustomMsgEx((CustomToServerFlag.CLEINT_CUSTOMMSG_REQUEST_CLEAR_CD ),[11]);
						}
					}
					break;
				case "funcBtn3"://元宝
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,12,4);
					LogSendLogic.getInstance().btnClickNumSend(22828)
					break;
				case "funcBtn4":
					break;
			}
		}
		
		private function onBtnOver(e:MouseEvent):void
		{
			(e.currentTarget as MovieClip).gotoAndStop(2);
			switch(e.currentTarget.name)
			{
				case "funcBtn1"://阅历
					TipsMainLogic.instance.showMain(StaticIni.getSwfLangStr("UIString_1000238"));
					break;
				case "funcBtn2":
					TipsMainLogic.instance.showMain(StaticIni.getSwfLangStr("UIString_1000195"));
					break;
				case "funcBtn3"://元宝
					TipsMainLogic.instance.showMain(StaticIni.getSwfLangStr("UIString_1000239"));
					break;
				case "funcBtn4":
					break;
			}
		}
		
		private function onBtnOut(e:MouseEvent):void
		{
			(e.currentTarget as MovieClip).gotoAndStop(1);
			TipsMainLogic.instance.hide();
		}
		
		public function update():void
		{
			RoleMc["lvText"].htmlText 		= 	npcModel.playerObj.GetPropertiesEx("Level");
			RoleMc["nameText"].htmlText 	= 	StaticIni.getSwfLangStr(npcModel.playerObj.GetPropertiesEx("NameID"));
			
			faceClip						=	new NxClip();
			faceClip.isCache				=	true;
			RoleMc["face"].addChild(faceClip);
			faceClip.source = ResPath.PHOTO + ResourcesPath.getHeadPic( npcModel.playerObj.ModelId ) + ".png";
			
			hpIndex							=	bind.BindProp(npcModel.playerObj,"HP",this,"changeBlood");
		}
		
		
		public function set vipStatus(level:String):void
		{
			var lvl:int				=	int(level);
			var VIPObj:Object		=	StaticIni.getIniObj("WorldBossUI_VIP");
			var autoBattleLevel:int	=	int(VIPObj["autoLevel"]);
			if( lvl >= autoBattleLevel)
			{
				RoleMc["check"].visible		=	true;
				RoleMc["check"].gotoAndStop(1);
				RoleMc["check"].buttonMode 	= true;
			}else
			{
				RoleMc["check"].visible		=	false;
				RoleMc["check"].stop();
			}
			
			
			var tempObj		:Object	=	null;
			while(tempObj == null)
			{
				tempObj	=	VIPObj["lvl"+lvl];
				lvl--;
			}
			lvl++;
			
			var btnArr:Array	=	String(VIPObj["lvl"+lvl]).split(",");
			var lenth:int		=	btnArr.length;
			initBtns();
			var totalWidth:int	=	RoleMc.width;
			var pieceWidtn:int	=	totalWidth/(lenth+1);
			for(var i:int=0;i<lenth;i++)
			{
				this["funcBtn"+btnArr[i]].visible	=	true;
				this["funcBtn"+btnArr[i]].x		=	RoleMc.x + pieceWidtn*(i+1);
			}
		}
		
		public function set changeBlood(val:String):void
		{
			var currentTime		:int		=	getTimer();
			var internalTime	:int		=	currentTime	-	lastTime;
			var currentHP		:int		=	int(val);
			var totalHP			:int		=	int(npcModel.playerObj.GetPropertiesEx("MaxHP"));
			RoleMc["hpText"].htmlText		=	currentHP + "/" + totalHP ;
			var hpLineWidth			:int	=	int(hpWidth * currentHP / totalHP);
			TweenLite.to(RoleMc["hpLine"],1,{width:hpLineWidth});
			
			var lostHP			:int		=	lastHP	-	currentHP;
			if(!isFirst && lostHP>0)
			{
				if(internalTime	>=	damageTime)
				{
					ShowDamageNum.ShowDamageEffect( lostHP , new Point( RoleMc.x+RoleMc.width-30, RoleMc.y+RoleMc.height-40 ) ,damageEffect);
				}
			}
			isFirst							=	false;
			
			lastHP							=	currentHP;
			lastTime						=	currentTime;
		}
	}
}
